package com.kjj;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.json.JSONObject;

import android.graphics.Bitmap;

public class Communication {

    public static final String POST_URL = "http://www.baixing.com/sampleCode/fabu/?action=mobilePost";
    public static final String UPLOAD_PIC_URL = "http://www.baixing.com/sampleCode/fabu/?action=mobilePost";
    public static final String DELETE_URL = "http://www.baixing.com/sampleCode/delete/";
    public static final String FEEDBACK_URL = "http://www.baixing.com/sampleCode/feedback/";

    public static String uploadPicture(Bitmap bmp) {
	System.out.println("uploadPicture");
	try {
	    URL url = new URL(UPLOAD_PIC_URL);
	    HttpURLConnection connection = (HttpURLConnection) url
		    .openConnection();
	    connection.setDoOutput(true);
	    connection.setRequestMethod("POST");
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
	    PrintWriter pw = new PrintWriter(new OutputStreamWriter(
		    connection.getOutputStream()));
	    pw.print(bos.toByteArray());
	    pw.flush();
	    pw.close();
	    InputStreamReader reader = new InputStreamReader(
		    connection.getInputStream());
	    BufferedReader buffer = new BufferedReader(reader);
	    String content = "", line = null;
	    while ((line = buffer.readLine()) != null) {
		content += line;
	    }
	    System.out.println("upload content: " + content);
	    reader.close();
	    connection.disconnect();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    public static int postProduct(String[] picurl, Product product) {
	System.out.println("postProduct : " + product.price + ","
		+ product.description);
	try {
	    URL url = new URL(POST_URL);
	    HttpURLConnection connection = (HttpURLConnection) url
		    .openConnection();
	    connection.setDoOutput(true);
	    connection.setRequestMethod("POST");
	    connection.setRequestProperty("Content-Type",
		    "application/x-www-form-urlencoded");
	    connection.connect();
	    DataOutputStream out = new DataOutputStream(
		    connection.getOutputStream());
	    String cmd = "mobile=" + ProductList.phoneNum;
	    cmd += "&form=mobilePost";
	    cmd += "&category=qitazhuanrang";
	    cmd += "&title=" + URLEncoder.encode(product.description, "UTF-8");
	    cmd += "&price" + product.price;
	    for (int i = 0; i < picurl.length; i++) {
		if (picurl[i] != null) {
		    cmd += "&image[]" + picurl[i];
		}
	    }
	    cmd += "&androidUniqueIdentifier" + ProductList.androidId;
	    out.writeBytes(cmd);
	    out.flush();
	    out.close();
	    InputStreamReader reader = new InputStreamReader(
		    connection.getInputStream());
	    BufferedReader buffer = new BufferedReader(reader);
	    String content = "", line = null;
	    while ((line = buffer.readLine()) != null) {
		content += line;
	    }
	    System.out.println("post content: " + content);
	    reader.close();
	    connection.disconnect();
	    JSONObject obj = new JSONObject(content);
	    System.out.println("id: " + obj.getString("id"));
	    System.out.println("title: " + obj.getString("title"));
	    System.out.println("dateStr: " + obj.getString("dateStr"));
	    System.out.println("link: " + obj.getString("link"));
	    System.out.println("status: " + obj.getString("status"));
	    System.out.println("imageFlag: " + obj.getString("imageFlag"));
	    product.id = obj.getString("id");
	    product.link = obj.getString("link");
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return 0;
    }

    public static boolean deleteProduct(Product product) {
	System.out.println("deleteProduct : " + product.price + ","
		+ product.description);
	try {
	    String cmd = DELETE_URL;
	    cmd += "?content=" + product.id;
	    cmd += "&androidUniqueIdentifier" + ProductList.androidId;
	    URL url = new URL(cmd);
	    HttpURLConnection connection = (HttpURLConnection) url
		    .openConnection();
	    connection.setDoOutput(true);
	    connection.setRequestMethod("GET");
	    InputStreamReader reader = new InputStreamReader(
		    connection.getInputStream());
	    BufferedReader buffer = new BufferedReader(reader);
	    String content = "", line = null;
	    while ((line = buffer.readLine()) != null) {
		content += line;
	    }
	    System.out.println("delete content: " + content);
	    reader.close();
	    connection.disconnect();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return true;
    }

    public static boolean sendFeedback(String str) {
	System.out.println("sendFeedback : " + str);
	try {
	    String cmd = FEEDBACK_URL;
	    cmd += "?content=" + URLEncoder.encode(str, "UTF-8");
	    cmd += "&androidUniqueIdentifier" + ProductList.androidId;
	    URL url = new URL(cmd);
	    HttpURLConnection connection = (HttpURLConnection) url
		    .openConnection();
	    connection.setDoOutput(true);
	    connection.setRequestMethod("GET");
	    InputStreamReader reader = new InputStreamReader(
		    connection.getInputStream());
	    BufferedReader buffer = new BufferedReader(reader);
	    String content = "", line = null;
	    while ((line = buffer.readLine()) != null) {
		content += line;
	    }
	    System.out.println("feedback content: " + content);
	    reader.close();
	    connection.disconnect();
	    JSONObject obj = new JSONObject(content);
	    System.out.println("result: " + obj.getString("result"));
	    System.out.println("errMsg: " + obj.getString("errMsg"));
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return true;
    }
}
